package com.wskeee.ec.components
{
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.interfaces.IToolTip;
	import com.wskeee.ec.styles.DefaultStyle;
	
	import flash.display.Graphics;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class ToolTip extends EComponent implements IToolTip
	{
		public function ToolTip()
		{
			super();
		}
		//--------------------------------------------------------------------------
		//
		//  varialbes
		//
		//--------------------------------------------------------------------------
		/**
		 * 最大宽度 
		 */
		private var maxWidth:Number = 300;
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		private var textFiled:TextField;
		
		//---------------------
		//text
		//---------------------
		private var _text:String = "";
		/**
		 * 设置显示 text 
		 * @param value
		 * 
		 */
		public function set text(value:String):void
		{
			if(_text!=value)
			{
				_text = value;
				invalidateProperties();
				invalidateSize();
			}
		}
		
		public function get text():String
		{
			return _text;
		}
		
		//--------------------------------------------------------------------------
		//
		//  method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  init
		//-----------------------------------------------------------------
		override protected function initChildren():void
		{
			super.initChildren();
			filters = [DefaultStyle.getShadow(2,false,45,0.5,5,5,0.5)];
			initTextFiled();
		}
		//-----------------------------------------------------------------
		//  initTextFiled
		//-----------------------------------------------------------------
		private function initTextFiled():void
		{
			textFiled = new TextField();
			textFiled.autoSize = TextFieldAutoSize.LEFT;
			
			var tf:TextFormat = new TextFormat();
			tf.font = "宋体";
			tf.size = getStyle("fontSize");
			textFiled.defaultTextFormat = tf;
			
			addChild(textFiled);
		}
		
		//---------------------
		//commitProperties
		//---------------------
		override protected function commitProperties():void
		{
			super.commitProperties();
			textFiled.visible = false;
			textFiled.text = text;
		}
		
		//---------------------
		//measure
		//---------------------
		override protected function measure():void
		{
			super.measure();
			measureWidth = textFiled.width > maxWidth ? maxWidth : textFiled.width;
			setActualSize(measureWidth,NaN);
		}
		
		//---------------------
		//updateDisplayList
		//---------------------
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			textFiled.wordWrap = width==maxWidth;
			textFiled.width = width-4;
			setActualSize(width,textFiled.height+4);
			textFiled.x = textFiled.y = 2;
			textFiled.visible = true;
			var g:Graphics = graphics;
			g.clear();
			g.lineStyle(1,getStyle("borderColor") as uint);
			g.beginFill(getStyle("backgroundColor") as uint,getStyle("backgroundAlpha") as Number);
			g.drawRoundRect(0,0,width,height,5,5);
			g.endFill();
		}
	}
}